package com.shanling.shanlingcontroller.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.shanling.shanlingcontroller.CustomApplication;
import com.shanling.shanlingcontroller.R;
import com.shanling.shanlingcontroller.adapter.CommonAdapter;
import com.shanling.shanlingcontroller.adapter.ViewHolder;
import com.shanling.shanlingcontroller.base.BaseAppManager;
import com.shanling.shanlingcontroller.bean.Constants;
import com.shanling.shanlingcontroller.bean.EventCenter;
import com.shanling.shanlingcontroller.bean.Music;
import com.shanling.shanlingcontroller.bean.MybluetoothDevice;
import com.shanling.shanlingcontroller.music.event.PlaylistEvent;
import com.shanling.shanlingcontroller.music.player.PlayManager;
import com.shanling.shanlingcontroller.music.player.SongLoader;
import com.shanling.shanlingcontroller.music.player.playqueue.PlayQueueManager;
import com.shanling.shanlingcontroller.ui.activity.MusicPlayActivity;
import com.shanling.shanlingcontroller.utils.ConvertUtils;
import com.shanling.shanlingcontroller.utils.CoverLoader;
import com.shanling.shanlingcontroller.utils.DpUtils;
import com.shanling.shanlingcontroller.utils.ToastUtils;

import org.greenrobot.eventbus.EventBus;
import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ch1 on 2019/11/20.
 */

public class MusicListPopup   {
    private CommonAdapter<Music> musicCommonAdapter;
    private PopupWindow popupWindow;
    private  View popupView;
    private Activity activity;

    public MusicListPopup(Activity activity) {
        this.activity = activity;
        initPopwwindow(activity);
    }

    private void initPopwwindow(Context mContext) {
        popupView = LayoutInflater.from(mContext).inflate(R.layout.music_listpop, null);
        popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.MATCH_PARENT, DpUtils.dpToPx( mContext,350), true);
        popupWindow.setTouchable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setFocusable(true);
        popupWindow.setAnimationStyle(R.style.PopupAnimation);
        popupWindow.setBackgroundDrawable(new ColorDrawable(mContext.getResources().getColor(R.color.bg_normal)));
        popupWindow.getContentView().setFocusableInTouchMode(true);
        popupWindow.getContentView().setFocusable(true);
        List<Music> musicList=new ArrayList<>();
        musicCommonAdapter=new CommonAdapter<Music>(mContext,musicList,R.layout.item_musiclistpop) {
            @Override
            public void convert(ViewHolder helper, int position, Music item) {
                TextView  tv_name = helper.getView(R.id.tv_name);
                ImageView iv_delete=helper.getView(R.id.iv_delete);
                tv_name.setText(item.getTitle()+"-"+item.getArtist());
                if(PlayManager.getPlayingMusic().getMid().equals(item.getMid())){
                    tv_name.setTextColor(0xffCAB487);
                }else {
                    tv_name.setTextColor(0xffffffff);
                }
                tv_name.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        PlayManager.play(position);
                        List<Music> playList = PlayManager.getPlayList();
                        refresh(playList);

                    }
                });
                iv_delete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        PlayManager.removeFromQueue(position);
                        List<Music> playList = PlayManager.getPlayList();
                        refresh(playList);
                        if(playList.size()==0){
                            ToastUtils.showToast(CustomApplication.getInstance(),R.string.playlist_is_empty);
                            popupWindow.dismiss();
                            BaseAppManager.getInstance().finishActivityclass(MusicPlayActivity.class);
                            return;
                        }
                        if(PlayManager.getPlayingMusic().getMid().equals(item.getMid())){
                            PlayManager.next();
                        }
                    }
                });
            }
        };

    }

    public void show(List<Music> musicList, View view){
        if(popupView!=null&&view!=null){
            ListView iv_cover=popupView.findViewById(R.id.lv_songs);
            ImageView iv_mode=popupView.findViewById(R.id.iv_mode);
            TextView tv_mode=popupView.findViewById(R.id.tv_mode);
            ImageView iv_delete=popupView.findViewById(R.id.deleteall);
            int playModeId = PlayQueueManager.INSTANCE.getPlayModeId();
            switch (playModeId){
                case  PlayQueueManager.PLAY_MODE_OREDE:
                    iv_mode.setImageResource(R.drawable.player_icon_order);
                    tv_mode.setText(R.string.play_mode_in_order);
                    break;
                case  PlayQueueManager.PLAY_MODE_REPEAT:
                    iv_mode.setImageResource(R.drawable.player_icon_single);
                    tv_mode.setText(R.string.play_mode_repeat);
                    break;
                case PlayQueueManager.PLAY_MODE_RANDOM:
                    iv_mode.setImageResource(R.drawable.player_icon_shuffle);
                    tv_mode.setText(R.string.play_mode_random);
                    break;
                case  PlayQueueManager.PLAY_MODE_LOOP:
                    iv_mode.setImageResource(R.drawable.player_icon_list_circulation);
                    tv_mode.setText(R.string.play_mode_loop);
                    break;
            }
            musicCommonAdapter.refresh(musicList);
            iv_cover.setAdapter(musicCommonAdapter);
            iv_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    PlayManager.clearQueue();
                    musicList.clear();
                    musicCommonAdapter.refresh(musicList);
                    ToastUtils.showToast(CustomApplication.getInstance(),R.string.playlist_is_empty);
                    popupWindow.dismiss();
                    BaseAppManager.getInstance().finishActivityclass(MusicPlayActivity.class);

                }
            });
            popupWindow.showAtLocation(view, Gravity.BOTTOM,0,0);
            bgAlpha(0.4f,activity);
            popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    bgAlpha(1.0f,activity);
                }
            });
        }
    }


    private  void bgAlpha(float f,Activity activity){
        WindowManager.LayoutParams layoutParams = activity.getWindow().getAttributes();
        layoutParams.alpha = f;
        activity.getWindow().setAttributes(layoutParams);
    }


}
